package com.epalmpay.service.ys.profit.impl;

import com.epalmpay.dto.yunliu.profit.*;
import com.epalmpay.mapper.AgentMapper;
import com.epalmpay.mapper.ProfitMapper;
import com.epalmpay.service.ys.profit.IYsProfitService;
import com.epalmpay.util.Logger;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;


/**
 * Created by Administrator on 2017/9/7.
 */

@Service("ysProfitService")
public class YsProfitServiceImpl implements IYsProfitService {

    protected Logger logger = Logger.getLogger(this.getClass());

    @Resource
    private ProfitMapper profitMapper;
    @Resource
    private AgentMapper agentMapper;

    public ObdProfitListListOutDTO getObdProfitList(ObdProfitListInDTO inDTO) throws Exception{
        ObdProfitListListOutDTO result = new ObdProfitListListOutDTO();

        Map<String,Object> param=new HashMap<String,Object>();
        param.put("groupId", inDTO.getGroupId());
        param.put("agentId", inDTO.getAgentId());
        param.put("activateStatus", inDTO.getActivateStatus());
        param.put("startDate", inDTO.getStartDate());//有待输入当前月份
        param.put("endDate", inDTO.getEndDate());
        param.put("name", inDTO.getName());

        int sumCount = profitMapper.selectOBDProfitListCount(param);
        result.setSumCount(sumCount);
        if(sumCount<=0)
            return result;

        param.put("limit", inDTO.getPageSize());
        param.put("start", inDTO.getPageSize()*(inDTO.getPageNo()-1));

        result.setObdProfitListOutDTOS(profitMapper.selectOBDProfitList(param));

        return result;
    }

    public YsProfitOutDTO getObdProfitMonthList(ObdProfitListInDTO inDTO) throws Exception{
        YsProfitOutDTO result = new YsProfitOutDTO();

        Map<String,Object> param=new HashMap<String,Object>();
        param.put("groupId", inDTO.getGroupId());
        param.put("agentId", inDTO.getAgentId());

        int sumCount = profitMapper.selectOBDProfitMonthListCount(param);
        result.setSumCount(sumCount);
        if(sumCount<=0)
            return result;

        param.put("limit", inDTO.getPageSize());
        param.put("start", inDTO.getPageSize()*(inDTO.getPageNo()-1));

        result.setObdProfitMonthListOutDTOS(profitMapper.selectOBDProfitMonthList(param));

        return result;
    }

    public YsProfitOutDTO getObdProfitDayList(ObdProfitListInDTO inDTO) throws Exception{
        YsProfitOutDTO result = new YsProfitOutDTO();

        Map<String,Object> param=new HashMap<String,Object>();
        param.put("groupId", inDTO.getGroupId());
        param.put("agentId", inDTO.getAgentId());
        param.put("statisDate", inDTO.getStatisDate());

        int sumCount = profitMapper.selectOBDProfitDayListCount(param);
        result.setSumCount(sumCount);
        if(sumCount<=0)
            return result;

        param.put("limit", inDTO.getPageSize());
        param.put("start", inDTO.getPageSize()*(inDTO.getPageNo()-1));

        result.setObdProfitDayListOutDTOS(profitMapper.selectOBDProfitDayList(param));

        return result;
    }

    public CreditProfitListListOutDTO getCreditProfitList(CreditProfitListInDTO inDTO) throws Exception{
        CreditProfitListListOutDTO result = new CreditProfitListListOutDTO();

        Map<String,Object> param=new HashMap<String,Object>();
        param.put("groupId", inDTO.getGroupId());
        param.put("agentId", inDTO.getAgentId());
        param.put("startDate", inDTO.getStartDate());//有待输入当前月份
        param.put("endDate", inDTO.getEndDate());
        param.put("name", inDTO.getName());

        int sumCount = profitMapper.selectCreditProfitListCount(param);
        result.setSumCount(sumCount);
        if(sumCount<=0)
            return result;

        param.put("limit", inDTO.getPageSize());
        param.put("start", inDTO.getPageSize()*(inDTO.getPageNo()-1));

        result.setCreditProfitListOutDTOS(profitMapper.selectCreditProfitList(param));

        return result;
    }

    public YsProfitOutDTO getCreditProfitMonthList(CreditProfitListInDTO inDTO) throws Exception{
        YsProfitOutDTO result = new YsProfitOutDTO();

        Map<String,Object> param=new HashMap<String,Object>();
        param.put("groupId", inDTO.getGroupId());
        param.put("agentId", inDTO.getAgentId());

        //取得代理商级别
        if(inDTO.getAgentId() != null){
            result.setAgentLevel(agentMapper.selectByPrimaryKey(inDTO.getAgentId()).getAgentLevel());
        }

        int sumCount = profitMapper.selectCreditProfitMonthListCount(param);
        result.setSumCount(sumCount);
        if(sumCount<=0)
            return result;

        param.put("limit", inDTO.getPageSize());
        param.put("start", inDTO.getPageSize()*(inDTO.getPageNo()-1));

        result.setCreditProfitMonthListOutDTOS(profitMapper.selectCreditProfitMonthList(param));

        return result;
    }

    public YsProfitOutDTO getCreditProfitDayList(CreditProfitListInDTO inDTO) throws Exception{
        YsProfitOutDTO result = new YsProfitOutDTO();

        Map<String,Object> param=new HashMap<String,Object>();
        param.put("groupId", inDTO.getGroupId());
        param.put("agentId", inDTO.getAgentId());
        param.put("statisDate", inDTO.getStatisDate());

        //取得代理商级别
        if(inDTO.getAgentId() != null){
            result.setAgentLevel(agentMapper.selectByPrimaryKey(inDTO.getAgentId()).getAgentLevel());
        }

        int sumCount = profitMapper.selectCreditProfitDayListCount(param);
        result.setSumCount(sumCount);
        if(sumCount<=0)
            return result;

        param.put("limit", inDTO.getPageSize());
        param.put("start", inDTO.getPageSize()*(inDTO.getPageNo()-1));

        result.setCreditProfitDayListOutDTOS(profitMapper.selectCreditProfitDayList(param));

        return result;
    }

    public YsProfitOutDTO getPolicyProfitList(PolicyProfitListInDTO inDTO) throws Exception{
        YsProfitOutDTO result = new YsProfitOutDTO();

        Map<String,Object> param=new HashMap<String,Object>();
        param.put("groupId", inDTO.getGroupId());
        param.put("agentId", inDTO.getAgentId());
        param.put("startDate", inDTO.getStartDate());//有待输入当前月份
        param.put("endDate", inDTO.getEndDate());
        param.put("name", inDTO.getName());

        int sumCount = profitMapper.selectPolicyProfitListCount(param);
        result.setSumCount(sumCount);
        if(sumCount<=0)
            return result;

        param.put("limit", inDTO.getPageSize());
        param.put("start", inDTO.getPageSize()*(inDTO.getPageNo()-1));

        result.setPolicyProfitListDTOS(profitMapper.selectPolicyProfitList(param));

        return result;
    }

    public YsProfitOutDTO getPolicyProfitMonthList(PolicyProfitListInDTO inDTO) throws Exception{
        YsProfitOutDTO result = new YsProfitOutDTO();

        Map<String,Object> param=new HashMap<String,Object>();
        param.put("groupId", inDTO.getGroupId());
        param.put("agentId", inDTO.getAgentId());

        //取得代理商级别
        if(inDTO.getAgentId() != null){
            result.setAgentLevel(agentMapper.selectByPrimaryKey(inDTO.getAgentId()).getAgentLevel());
        }

        int sumCount = profitMapper.selectPolicyProfitMonthListCount(param);
        result.setSumCount(sumCount);
        if(sumCount<=0)
            return result;

        param.put("limit", inDTO.getPageSize());
        param.put("start", inDTO.getPageSize()*(inDTO.getPageNo()-1));

        result.setPolicyProfitMonthListOutDTOS(profitMapper.selectPolicyProfitMonthList(param));

        return result;
    }

    public YsProfitOutDTO getPolicyProfitDayList(PolicyProfitListInDTO inDTO) throws Exception{
        YsProfitOutDTO result = new YsProfitOutDTO();

        Map<String,Object> param=new HashMap<String,Object>();
        param.put("groupId", inDTO.getGroupId());
        param.put("agentId", inDTO.getAgentId());
        param.put("statisDate", inDTO.getStatisDate());

        //取得代理商级别
        if(inDTO.getAgentId() != null){
            result.setAgentLevel(agentMapper.selectByPrimaryKey(inDTO.getAgentId()).getAgentLevel());
        }

        int sumCount = profitMapper.selectPolicyProfitDayListCount(param);
        result.setSumCount(sumCount);
        if(sumCount<=0)
            return result;

        param.put("limit", inDTO.getPageSize());
        param.put("start", inDTO.getPageSize()*(inDTO.getPageNo()-1));

        result.setPolicyProfitDayListOutDTOS(profitMapper.selectPolicyProfitDayList(param));

        return result;
    }

}
